/**
 * 新增时间：2009-12-07
 * 朱家进
 * 新增内容：增加市级社会渠道省组织和市组织
 */

package com.zjht.gmcc.servlet.sys;

import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zjht.gmcc.bean.BaseServlet;
import com.zjht.gmcc.bean.util.InitParam;
import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyJDBC;
import com.zjht.gmcc.bean.util.MyParamUtil;
import com.zjht.gmcc.bean.util.MySession;

public class OrgaAddCityPro extends BaseServlet {

	private static final long serialVersionUID = 1L;

	public boolean process(HttpServletRequest request, HttpServletResponse response, HttpSession session, PrintWriter out, MyJDBC myJDBC, String menu)
			throws Exception {

		String porgatype = request.getParameter("porgaType");// 父组织类型
		// String porgacode = request.getParameter("porgacode");// 父组织编码

		String orgacode = request.getParameter("orgacode").trim().toUpperCase();// 新添加组织编码
		String organame = request.getParameter("organame");// 新添加组织名称

		String kind = request.getParameter("kind");// 标志位，添加省级组织 == 1 否则为添加下级组织
		String actorcode = request.getParameter("actorcode");// 新添 例:W
		String channelType = request.getParameter("channelType");// 渠道类型

		int flag = 0;
		PreparedStatement ps = null;

		String shortname = request.getParameter("shortname");
		String linkman = request.getParameter("linkman");
		String postcode = request.getParameter("postcode");
		String fax = request.getParameter("fax");
		String tel = request.getParameter("tel");
		String address = request.getParameter("address");
		String status = (request.getParameter("status") == null) ? "0" : request.getParameter("status");
		String arealist = request.getParameter("arealist");

		if (porgatype == null || porgatype.length() == 0) {
			alert(out, "无法新增组织信息，请与管理员联系。");
			return false;
		}

		String sql = "";

		String areaid = String.valueOf(MySession.getAreaid(session));
		String areaShortName = null;

		String areaidSql = "SELECT shortname FROM area WHERE areaid = ?";
		PreparedStatement areaidPs = myJDBC.prepareStatement(areaidSql);
		areaidPs.setString(1, areaid);
		ResultSet areaidRs = areaidPs.executeQuery();
		if (areaidRs.next()) {
			areaShortName = areaidRs.getString("shortname");
		}
		if (areaidRs != null)
			areaidRs.close();

		String provorgid = String.valueOf(MyParamUtil.getSEQ("seq_orgid"));
		String cityorgid = String.valueOf(MyParamUtil.getSEQ("seq_orgid"));

		if (kind != null)// 添加省级组织
		{
			sql = "INSERT INTO organiza(orgacode, organame, parentorgacode, orgatypeid, actorcode, "
					+ "areaid, address, postcode, linkman, telephone, fax, createdate, "
					+ "shortname, paraid, status ,fillhour,channeltype,orgid,parentorgid) "
					+ "VALUES(?,?,0,?,?,?,?,?,?,?,?,sysdate,?,0,?,null,?,?,-1)";
			ps = myJDBC.prepareStatement(sql);
			ps.setString(1, areaShortName + "A" + orgacode);
			ps.setString(2, organame);
			ps.setString(3, porgatype);
			ps.setString(4, actorcode);
			ps.setString(5, areaid);
			ps.setString(6, address);
			ps.setString(7, postcode);
			ps.setString(8, linkman);
			ps.setString(9, tel);
			ps.setString(10, fax);
			ps.setString(11, shortname);
			ps.setString(12, status);
			ps.setString(13, channelType);
			ps.setString(14, provorgid);

			flag = ps.executeUpdate();
			ps.close();
		}

		if (flag == 1) {
			sql = "INSERT INTO organiza(orgacode, organame, parentorgacode, orgatypeid, actorcode, "
					+ "areaid, address, postcode, linkman, telephone, fax, createdate, "
					+ "shortname, paraid, status ,fillhour,channeltype,orgid,parentorgid) "
					+ "VALUES(?,?,?,?,?,?,?,?,?,?,?,sysdate,?,0,?,null,?,?,?)";
			ps = myJDBC.prepareStatement(sql);
			ps.setString(1, arealist.split(",")[1] + "B" + orgacode);
			ps.setString(2, organame);
			ps.setString(3, areaShortName + "A" + orgacode);
			ps.setString(4, "6");
			ps.setString(5, actorcode);
			ps.setString(6, arealist.split(",")[0]);
			ps.setString(7, address);
			ps.setString(8, postcode);
			ps.setString(9, linkman);
			ps.setString(10, tel);
			ps.setString(11, fax);
			ps.setString(12, shortname);
			ps.setString(13, status);
			ps.setString(14, channelType);
			ps.setString(15, cityorgid);
			ps.setString(16, provorgid);

			flag = ps.executeUpdate();
			ps.close();
		}

		String opersDutySql = "INSERT INTO OPERSPOPEDOM (opercode,dutyid,operid) VALUES (?,?,?)";
		PreparedStatement operDutyPs = myJDBC.prepareStatement(opersDutySql);

		String opersSql = "INSERT INTO opers(opercode, opername, passwd, orgacode, deptid,createdate, status, opertype, showflag,operid,orgid) "
				+ "VALUES(?,?,?,?,?,sysdate,1,1,1,?,?)";
		PreparedStatement operPs = myJDBC.prepareStatement(opersSql);

		String jmpassword = MyConstant.INIT_PWD;
		String provOrgacode = areaShortName.toUpperCase() + "A" + orgacode;// 省仓编码
		String cityOrgacode = areaShortName.toUpperCase() + "B" + orgacode;// 市仓编码
		String provJL = areaShortName.toUpperCase() + "A" + orgacode + "1";// 省仓经理
		String provCG = areaShortName.toUpperCase() + "A" + orgacode + "2";// 省仓仓管
		String cityJL = areaShortName.toUpperCase() + "B" + orgacode + "1";// 市仓经理
		String cityCG = areaShortName.toUpperCase() + "B" + orgacode + "2";// 市仓仓管

		String operid = String.valueOf(MyParamUtil.getSEQ("seq_operid"));
		if (flag == 1) {
			operPs.setString(1, provJL);
			operPs.setString(2, "省仓经理");
			operPs.setString(3, jmpassword);
			operPs.setString(4, provOrgacode);
			operPs.setString(5, "50");
			operPs.setString(6, operid);
			operPs.setString(7, provorgid);
			flag = operPs.executeUpdate();
		}
		if (flag == 1) {
			operDutyPs.setString(1, provJL);
			operDutyPs.setString(2, "5000");
			operDutyPs.setString(3, operid);
			flag = operDutyPs.executeUpdate();
		}

		operid = String.valueOf(MyParamUtil.getSEQ("seq_operid"));
		if (flag == 1) {
			operPs.setString(1, provCG);
			operPs.setString(2, "省仓仓管");
			operPs.setString(3, jmpassword);
			operPs.setString(4, provOrgacode);
			operPs.setString(5, "53");
			operPs.setString(6, operid);
			operPs.setString(7, provorgid);
			flag = operPs.executeUpdate();
		}
		if (flag == 1) {
			operDutyPs.setString(1, provCG);
			operDutyPs.setString(2, "5300");
			operDutyPs.setString(3, operid);
			flag = operDutyPs.executeUpdate();
		}

		operid = String.valueOf(MyParamUtil.getSEQ("seq_operid"));
		if (flag == 1) {
			operPs.setString(1, cityJL);
			operPs.setString(2, "市仓经理");
			operPs.setString(3, jmpassword);
			operPs.setString(4, cityOrgacode);
			operPs.setString(5, "60");
			operPs.setString(6, operid);
			operPs.setString(7, cityorgid);
			flag = operPs.executeUpdate();
		}
		if (flag == 1) {
			operDutyPs.setString(1, cityJL);
			operDutyPs.setString(2, "6000");
			operDutyPs.setString(3, operid);
			flag = operDutyPs.executeUpdate();
		}

		operid = String.valueOf(MyParamUtil.getSEQ("seq_operid"));
		if (flag == 1) {
			operPs.setString(1, cityCG);
			operPs.setString(2, "市仓仓管");
			operPs.setString(3, jmpassword);
			operPs.setString(4, cityOrgacode);
			operPs.setString(5, "63");
			operPs.setString(6, operid);
			operPs.setString(7, cityorgid);
			flag = operPs.executeUpdate();
		}
		if (flag == 1) {
			operDutyPs.setString(1, cityCG);
			operDutyPs.setString(2, "6300");
			operDutyPs.setString(3, operid);
			flag = operDutyPs.executeUpdate();
		}

		String act = organame + " 添加";
		if (flag > 0) {
			InitParam.initOrgaInfo();
			if (kind != null)// 添加省级组织
			{
				alertOK(out, act + "成功！", "dialogArguments.parent.orgaleftFrame.location.reload();window.close();");
			} else {
				alertOK(out, act + "成功！", "dialogArguments.location.href='orga/orga.jsp?orgacode=" + orgacode + "&channelType = " + channelType
						+ "&orgaType=" + (Integer.parseInt(porgatype) + 1) + "';window.returnValue='" + orgacode + "';window.close();");
			}

		} else {
			errAlert(request, out, menu, act + "失败");
		}
		return flag > 0;

	}
}
